<?php
class PicturesCtl extends BaseCtl {
	public function PicturesCtl(){
		parent::BaseCtl();
	}
	
	public function edit(){
		try {
			// get the id, then get the stuff from the form and start the other page in
			// edit mode
			$id = $this->getPost('idedit');
			
			$form = $this->getPicturesForm(); 
			$picture = $form->findPicture($id);
			
			if($picture == null){
				throw new Exception("Cannot find the picture with id:{$id}");
			}
			else {
				$edit = $this->getEditPictureForm(); 
				
				$edit->clear();
				$edit->setAdding(false);
				$edit->setId($id);
				$edit->setName($picture->getComments());
				
				$edit->setAlbumId($form->getAlbumId());
				$edit->setAlbumName($form->getAlbumName());
				
				$this->redirect('editpicture.php');
			}
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
	
	public function add(){
		try {
			$form = $this->getPicturesForm();
			$albumId = $form->getAlbumId();
			$albumName = $form->getAlbumName();
			
			$frm = $this->getEditPictureForm();
			$frm->clear();
			$frm->setAdding(true);
			$service = new Service();
			
			$frm->setId($service->getNewId());
			$frm->setAlbumId($albumId);
			$frm->setAlbumName($albumName);
			
			$this->redirect('editpicture.php');
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
	
	public function multiple(){
		try {
			$form = $this->getPicturesForm();
			$albumId = $form->getAlbumId();
			$albumName = $form->getAlbumName();
			
			$frm = $this->getMUploadForm(); 
			$frm->clear();
			
			$frm->setAlbumId($albumId);
			$frm->setAlbumName($albumName);
			
			$this->redirect('mupload.php');
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
	
	public function delete(){
		try {
			$form = $this->getPicturesForm(); 
			
			$selected = $form->getSelected();
			if(is_array($selected) && count($selected) > 0){
				$pictures = $form->getSelectedPictures($selected);
				
				$frm = $this->getConfirmPictureForm(); 
				$frm->clear();
				
				$frm->setPictures($pictures);
				$this->redirect('confirmpicture.php');
			}
			else {
				throw new Exception("Please select at least one picture to delete");
			}
			
		}
		catch(Exception $e){
			HtmlUtil::addError($e->getMessage());
		}
	}
	
    public function isPostback(){
    	return 'pictures' == $this->getPost('pagename');
    }

    public function loadData(){
    	$form = $this->getPicturesForm();
    	
    	$form->setAlbumId($this->getPost('albumid'));
    	$form->setAlbumName($this->getPost('albumname'));
    	
    	// load the selected stuff
    	$selected = $this->getPost('selected');
    	$sel = $this->getNonEmpty($selected);
    	$form->setSelected($sel);
    }
    
    public function albumDefault(){
    	// the function makes a picture as being default
    	try {
			$form = $this->getPicturesForm();
			$albumId = $form->getAlbumId(); 
			$selected = $form->getSelected();
			
			if(is_array($selected) && count($selected) > 0){
				$id = $selected[0]; 
				
				$service = new Service();
				$service->setAlbumDefaultId($albumId, $id);
				
				// then update the albums form
				$alb = $this->getAlbumsForm();
				$alb->updateAlbumDefaultPicture($albumId, $id);
				
				HtmlUtil::addMessage("The default picture has been set");
			}
			else {
				throw new Exception("Please select at least one picture to delete");
			}
    	}
    	catch(Exception $e){
    		HtmlUtil::addError($e->getMessage());
    	}
    }
	
} 
?>